package me.monster.smarttraffic.tool;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

public class KvUtil {
    private static final String sp_name = "SmartTraffic";
    private static KvUtil instance;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private KvUtil() {
        sp = Util.getApp().getSharedPreferences(sp_name, Context.MODE_PRIVATE);
    }

    public static KvUtil getInstance() {
        if (instance == null) {
            instance = new KvUtil();
        }
        return instance;
    }

    public int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    @SuppressLint("CommitPrefEdits")
    public KvUtil editor() {
        editor = sp.edit();
        return this;
    }

    public KvUtil put(String key, String value) {
        editor.putString(key, value);
        return this;
    }

    public KvUtil put(String key, int value) {
        editor.putInt(key, value);
        return this;
    }

    public void apply() {
        editor.apply();
    }

    public String getString(String key, String value) {
        return sp.getString(key, value);
    }
}